﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
namespace AnyOffice.general.address.personal
{
    public partial class query : ModulePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                ListItem item = new ListItem();
                item = new ListItem("所有", "all");
                this.ddlGroupId.Items.Add(item);
                item = new ListItem("默认", "0");
                this.ddlGroupId.Items.Add(item);
                DataTable table = new AddressGroupDao().SelectAll(base.Conn, base.User.Identity.Name);
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        item = new ListItem(table.Rows[i]["GroupName"].ToString(), table.Rows[i]["GroupId"].ToString());
                        this.ddlGroupId.Items.Add(item);
                    }
                }
            }
            if (this.Page.IsPostBack)
            {
                this.litNavName.Text = "查询结果";
                this.pnlMain.Visible = false;
                this.pnlQuery.Visible = true;
                SqlCommand command = new SqlCommand();
                base.Conn.Open();
                command.Connection = base.Conn;
                string str = "SELECT * FROM Address ";
                str = str + " WHERE UserId = '" + base.User.Identity.Name + "' ";
                if (this.ddlGroupId.SelectedValue.ToUpper() != "ALL")
                {
                    str = str + " AND GroupId = @GroupId ";
                    command.Parameters.Add("@GroupId", Convert.ToInt32(this.ddlGroupId.SelectedValue));
                }
                if (this.txtName.Text != string.Empty)
                {
                    str = str + " AND [Name] LIKE '%' + @Name + '%' ";
                    command.Parameters.Add("@Name", this.txtName.Text);
                }
                if (this.ddlSex.SelectedValue.ToUpper() != "ALL")
                {
                    str = str + " AND Sex = @Sex ";
                    command.Parameters.Add("@Sex", Convert.ToBoolean(this.ddlSex.SelectedValue));
                }
                if (this.txtNickName.Text != string.Empty)
                {
                    str = str + " AND NickName LIKE '%' + @NickName + '%' ";
                    command.Parameters.Add("@NickName", this.txtNickName.Text);
                }
                if (this.txtDeptName.Text != string.Empty)
                {
                    str = str + " AND DeptName LIKE '%' + @DeptName + '%' ";
                    command.Parameters.Add("@DeptName", this.txtDeptName.Text);
                }
                if (this.txtDeptAddr.Text != string.Empty)
                {
                    str = str + " AND DeptAddr LIKE '%' + @DeptAddr + '%' ";
                    command.Parameters.Add("@DeptAddr", this.txtDeptAddr.Text);
                }
                if (this.txtHomeAddr.Text != string.Empty)
                {
                    str = str + " AND HomeAddr LIKE '%' + @HomeAddr + '%' ";
                    command.Parameters.Add("@HomeAddr", this.txtHomeAddr.Text);
                }
                if (this.txtNote.Text != string.Empty)
                {
                    str = str + " AND Note LIKE '%' + @Note + '%' ";
                    command.Parameters.Add("@Note", this.txtNote.Text);
                }
                command.CommandText = str;
                SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    this.rptAddress.DataSource = reader;
                    this.rptAddress.DataBind();
                    reader.Close();
                }
                else
                {
                    this.litMessage.Text = base.ShowMessage("无符合条件的联系人");
                }
            }
        }

    }
}
